package com.cobo.common.core;

import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class ClassPathUtils {

    public static List<String> readFromResourcePath(String path) {
        ClassLoader classLoader = ClassPathUtils.class.getClassLoader();
        InputStream inputStream = classLoader.getResourceAsStream(path);

        if (inputStream != null) {
            try {
                return IOUtils.readLines(inputStream, "utf-8");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
//            try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
//                String line;
//                while ((line = reader.readLine()) != null) {
//                    System.out.println(line);
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
        } else {
            throw new RuntimeException("File not found!");
        }
    }

    public static String readStrFromResourcePath(String path) {
        return readFromResourcePath(path).stream().reduce((t1, t2) -> t1 + "\n" + t2).orElse("");
    }
}
